package com.uyaer.engine.display
{
	import com.uyaer.engine.Engine;
	import com.uyaer.engine.core.Bean;
	import com.uyaer.engine.plug.Utils;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;

	public class Card extends Icon
	{
		public var clickHandler:Function;
		public var dbClickHandler:Function;
		public var dragStartHandler:Function;
		public var dragEndHandler:Function;
		
		private var startTime:Number;
		private var firstClickTime:Number;
		private var clickId:uint;
		private var cd:int;
		private var timer:Timer;
		public var maskSp:Sprite;
		public var clone:Sprite;
		public function Card(bean:Bean, size:int=42)
		{
			super(bean,size);
			maskSp = new Sprite();
			maskSp.visible = false;
			addChild(maskSp);
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, _onTimer);
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,_onDown);
			this.addEventListener(MouseEvent.CLICK,_onClick);
		}
		
		private function _onClick(e:MouseEvent):void{
			this.removeEventListener(MouseEvent.MOUSE_OUT,_onOut);
			if(firstClickTime){
				var now:Number = new Date().time;
				var t:Number = now - firstClickTime;
				if(t < 300){
					if(clickId){
						clearTimeout(clickId);
					}
					dbClickHandler && dbClickHandler(this);
				}
				firstClickTime = 0;
			}else{
				firstClickTime = new Date().time;
				clickId = setTimeout(_clickTimeFun,300);
			}
		}
		
		private function _clickTimeFun():void{
			firstClickTime = 0;
			clickHandler && clickHandler(this);
		}
		
		private function _onDown(e:MouseEvent):void{
			isCanHover = false;
			this.addEventListener(MouseEvent.MOUSE_OUT,_onOut);
		}
		
		private function _onOut(e:MouseEvent):void{
			if(!clone){
				clone = new Sprite();
			}
			clone.addChild((bm && new Bitmap(bm.bitmapData.clone())) || (animMc)); 
			stage.addChild(clone);
			stage.addEventListener(MouseEvent.MOUSE_UP, _onStageMouseUp);
			clone.startDrag(true);
			dragStartHandler && dragStartHandler(this);
		}
		
		private function _onStageMouseUp(e:MouseEvent):void{
			if(clone){
				clone.stopDrag();
				clone.parent && clone.parent.removeChild(clone);
				dragEndHandler && dragEndHandler(this);
			}
		}
		
		private function _onTimer(e:TimerEvent):void{
			var now:Number = new Date().time;
			var t:Number = now - startTime;
			if(t >= cd){
				maskSp.height = 0;
				endCD();
			}else{
				var h:Number = (cd - t)/cd * size;
				updateMask(h);
			}
		}
		
		private function updateMask(h:Number):void{
			maskSp.graphics.clear();
			maskSp.graphics.beginFill(0,0.4);
			maskSp.graphics.drawRect(0, size - h, size, h);
			maskSp.graphics.endFill();
		}
		
		public function startCD(cd:int):void{
			this.cd = cd * 1000;
			startTime = new Date().time;
			maskSp.visible = true;
			updateMask(size);
			if(!timer.running){
				timer.start();
			}
		}
		
		public function endCD():void{
			maskSp.visible = false;	
			timer.stop();
			
//			动画播放
		}
	}
}